<?php

namespace App\Listeners;

use App\Events\OrderSubmitEvent;
use App\Forms\GoodsForms;
use App\Models\Good;
use App\Models\GoodsSkuAttribute;
use App\Models\Order;
use App\Models\OrderDetail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class OrderSubmitListeners
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  OrderSubmitEvent  $event
     * @return void
     */
    public function handle(OrderSubmitEvent $event)
    {
        //
        $order = $event->order;

        //更新订单状态
        $order->update([
            'status' => Order::DELIVER,
            'pay_time' => date('Y-m-d H:i:s')
        ]);

        //发送模板消息 、 短信提醒

        //TODO 更新商品销量 后续需要移动到Forms层
        foreach ($order->details as $item){
            /**
             * @var $item OrderDetail
             */
            Good::whereKey($item->good_id)->increment('sales', $item->goods_count);
        }



    }
}
